//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.ares.app.demo.common.cfg;

import java.util.HashSet;
import javax.annotation.Resource;
import org.ares.app.demo.daos.UserDao;
import org.ares.app.demo.entities.SUser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.AuthorizedUrl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.util.StringUtils;

@Configuration
@EnableWebSecurity
public class DemoWebSecCfg extends WebSecurityConfigurerAdapter {
    @Resource
    UserDao ud;

    public DemoWebSecCfg() {
    }

    protected void configure(HttpSecurity http) throws Exception {
        ((HttpSecurity)((FormLoginConfigurer)((FormLoginConfigurer)((FormLoginConfigurer)((HttpSecurity)((AuthorizedUrl)((AuthorizedUrl)http.authorizeRequests().anyRequest()).authenticated().antMatchers(new String[]{"/**"})).hasRole("admin").and()).formLogin().loginProcessingUrl("/login")).loginPage("/userlogin").defaultSuccessUrl("/")).permitAll()).and()).logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/userlogin").permitAll();
    }

    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(new String[]{"/js/**", "/css/**", "/image*/**", "/**/favicon.ico", "/h2-console/**"});
        web.ignoring().antMatchers(new String[]{"/api/user/regisger", "/api/**", "/action/**"});
        web.ignoring().antMatchers(new String[]{"/transportservice/action/**", "/simusand/api/v2/**"});
    }

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(new UserDetailsService() {
            public UserDetails loadUserByUsername(String uname) throws UsernameNotFoundException {
                if (StringUtils.isEmpty(uname)) {
                    throw new UsernameNotFoundException("username is null");
                } else {
                    SUser var2 = DemoWebSecCfg.this.ud.findByUsername(uname);
                    if (var2 == null) {
                        throw new UsernameNotFoundException("username not found");
                    } else {
                        HashSet var3 = new HashSet();
                        var3.add(new SimpleGrantedAuthority(var2.getRole()));
                        return new User(uname, var2.getPassword(), !StringUtils.isEmpty(var2.getRole()), true, true, true, var3);
                    }
                }
            }
        });
    }

    @Bean
    @Scope(
            value = "session",
            proxyMode = ScopedProxyMode.TARGET_CLASS
    )
    public SUser curLoginUser() {
        SUser var1 = null;
        Authentication var2 = SecurityContextHolder.getContext().getAuthentication();
        Object var3 = null;
        if (var2 != null) {
            var3 = var2.getPrincipal();
        }

        if (var3 != null && var3 instanceof UserDetails) {
            String var4 = ((UserDetails)var3).getUsername();
            var1 = (SUser)this.ud.findOne(var4);
        }

        return var1;
    }
}
